//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Globalization;
using Tathata.Common.BLL.Enums;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using Tathata.Common.Configuration;

namespace Tathata.Common.Utils
{
    /// <summary>
    /// Represents a SEO helper
    /// </summary>
    public class SEOHelper
    {
        #region Methods
        /// <summary>
        /// Renders page meta tag
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="name">Meta name</param>
        /// <param name="content">Content</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderMetaTag(Page page, string name, string content, bool OverwriteExisting)
        {
            foreach (Control control in page.Header.Controls)
                if (control is HtmlMeta)
                {
                    HtmlMeta meta = (HtmlMeta)control;
                    if (meta.Name.ToLower().Equals(name.ToLower()) && !string.IsNullOrEmpty(content))
                    {
                        if (OverwriteExisting)
                            meta.Content = content;
                        else
                        {
                            if (String.IsNullOrEmpty(meta.Content))
                                meta.Content = content;
                        }
                    }
                }
        }

        /// <summary>
        /// Renders page title
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="title">Page title</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderTitle(Page page, string title, bool OverwriteExisting)
        {
            if (String.IsNullOrEmpty(title))
                return;

            if (OverwriteExisting)
                page.Title = title;
            else
            {
                if (String.IsNullOrEmpty(page.Title))
                    page.Title = title;
            }
        }

        /// <summary>
        /// Murges SE name
        /// </summary>
        /// <param name="name">Name</param>
        /// <returns>Result</returns>
        public static string MungeSEName(string name)
        {
            if (String.IsNullOrEmpty(name))
                return string.Empty;
            string OKChars = "abcdefghijklmnopqrstuvwxyz1234567890 _-";
            name = name.Trim().ToLowerInvariant();
            StringBuilder tmpS = new StringBuilder();
            foreach (char c in name.ToCharArray())
                if (OKChars.Contains(c.ToString()))
                    tmpS.Append(c);
            string name2 = tmpS.ToString();
            name2 = name2.Replace(" ", "-");
            while (name2.Contains("--"))
                name2 = name2.Replace("--", "-");
            while (name2.Contains("__"))
                name2 = name2.Replace("__", "_");
            return HttpContext.Current.Server.UrlEncode(name2);
        }

        /// <summary>
        /// Gets product URL
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product URL</returns>
        public static string GetProductURL(int ProductID)
        {
            Product product = ProductManager.GetByProductID(ProductID);
            if (product == null)
                throw new Exception(string.Format("Product couldn't be loaded. ID={0}", ProductID));
            string seName = MungeSEName(product.Name);
            string url = string.Format("{0}Products/{1}-{2}.aspx", CommonHelper.GetStoreHTTPLocation(false), ProductID, seName);
            return url;
        }

        /// <summary>
        /// Gets product email a friend URL
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product email a friend URL</returns>
        public static string GetProductEmailAFriendURL(int ProductID)
        {
            Product product = ProductManager.GetByProductID(ProductID);
            if (product == null)
                throw new Exception(string.Format("Product couldn't be loaded. ID={0}", ProductID));
            string url = string.Format("{0}ProductEmailAFriend.aspx?ProductID={1}", CommonHelper.GetStoreHTTPLocation(false), ProductID);
            return url;
        }

        /// <summary>
        /// Gets manufacturer URL
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <returns>Manufacturer URL</returns>
        public static string GetManufacturerURL(int ManufacturerID)
        {
            Manufacturer manufacturer = ManufacturerManager.GetByManufacturerID(ManufacturerID);
            if (manufacturer == null)
                throw new Exception(string.Format("Manufacturer couldn't be loaded. ID={0}", ManufacturerID));
            string seName = MungeSEName(manufacturer.Name);
            string url = string.Format("{0}Manufacturer/{1}-{2}.aspx", CommonHelper.GetStoreHTTPLocation(false), ManufacturerID, seName);
            return url;
        }

        /// <summary>
        /// Gets category URL
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        /// <returns>Category URL</returns>
        public static string GetCategoryURL(int CategoryID)
        {
            Category category = CategoryManager.GetByCategoryID(CategoryID);
            if (category == null)
                throw new Exception(string.Format("Category couldn't be loaded. ID={0}", CategoryID));
            string seName = MungeSEName(category.Name);
            string url = string.Format("{0}Category/{1}-{2}.aspx", CommonHelper.GetStoreHTTPLocation(false), CategoryID, seName);
            return url;
        }

        #endregion
    }
}
